#!/bin/bash

#############################################################################
## 
##   Logtalk macOS installer package auxiliary script
##   Last updated on November 1, 2023
## 
##   This file is part of Logtalk <https://logtalk.org/>  
##   SPDX-FileCopyrightText: 1998-2025 Paulo Moura <pmoura@logtalk.org>
##   SPDX-License-Identifier: Apache-2.0
##   
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##   
##       http://www.apache.org/licenses/LICENSE-2.0
##   
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.
## 
#############################################################################

echo ""
echo "Checking for /opt/local/bin in the system PATH..."
echo ""

if grep "/opt/local/bin" /etc/zprofile >/dev/null 2>&1; then
	echo "System PATH already includes /opt/local/bin"
else
	echo '' >> /etc/zprofile
	echo '# Links to the Logtalk scripts are created in /opt/local/bin' >> /etc/zprofile
	echo 'export PATH=$PATH:/opt/local/bin' >> /etc/zprofile
fi

if grep "/opt/local/bin" /etc/profile >/dev/null 2>&1; then
	echo "System PATH already includes /opt/local/bin"
else
	echo '' >> /etc/profile
	echo '# Links to the Logtalk scripts are created in /opt/local/bin' >> /etc/profile
	echo 'export PATH=$PATH:/opt/local/bin' >> /etc/profile
fi

if grep "/opt/local/bin" /etc/csh.cshrc >/dev/null 2>&1; then
	echo "System PATH already includes /opt/local/bin"
else
	echo '' >> /etc/csh.cshrc
	echo '# Links to the Logtalk scripts are created in /opt/local/bin' >> /etc/csh.cshrc
	echo 'setenv PATH "${PATH}":/opt/local/bin' >> /etc/csh.cshrc
fi

echo ""
echo "Checking for /opt/local/man in the system MANPATH..."
echo ""

if grep "/opt/local/man" /etc/zprofile >/dev/null 2>&1; then
	echo "System MANPATH already includes /opt/local/man"
else
	echo '' >> /etc/zprofile
	echo '# Links to the Logtalk man pages are created in /opt/local/man' >> /etc/zprofile
	echo 'export MANPATH=$MANPATH:/opt/local/man' >> /etc/zprofile
fi

if grep "/opt/local/man" /etc/profile >/dev/null 2>&1; then
	echo "System MANPATH already includes /opt/local/man"
else
	echo '' >> /etc/profile
	echo '# Links to the Logtalk man pages are created in /opt/local/man' >> /etc/profile
	echo 'export MANPATH=$MANPATH:/opt/local/man' >> /etc/profile
fi

if grep "/opt/local/man" /etc/csh.cshrc >/dev/null 2>&1; then
	echo "System MANPATH already includes /opt/local/man"
else
	echo '' >> /etc/csh.cshrc
	echo '# Links to the Logtalk man pages are created in /opt/local/man' >> /etc/csh.cshrc
	echo 'setenv MANPATH "${MANPATH}":/opt/local/man' >> /etc/csh.cshrc
fi

echo ""
echo "Checking for /opt/local/share/info in the system INFOPATH..."
echo ""

if grep "/opt/local/share/info" /etc/zprofile >/dev/null 2>&1; then
	echo "System INFOPATH already includes /opt/local/share/info"
else
	echo '' >> /etc/zprofile
	echo '# Links to the Logtalk Texinfo files are created in /opt/local/share/info' >> /etc/zprofile
	echo 'export INFOPATH=$INFOPATH:/opt/local/share/info' >> /etc/zprofile
fi

if grep "/opt/local/share/info" /etc/profile >/dev/null 2>&1; then
	echo "System INFOPATH already includes /opt/local/share/info"
else
	echo '' >> /etc/profile
	echo '# Links to the Logtalk Texinfo files are created in /opt/local/share/info' >> /etc/profile
	echo 'export INFOPATH=$INFOPATH:/opt/local/share/info' >> /etc/profile
fi

if grep "/opt/local/share/info" /etc/csh.cshrc >/dev/null 2>&1; then
	echo "System INFOPATH already includes /opt/local/share/info"
else
	echo '' >> /etc/csh.cshrc
	echo '# Links to the Logtalk Texinfo files are created in /opt/local/share/info' >> /etc/csh.cshrc
	echo 'setenv INFOPATH "${INFOPATH}":/opt/local/share/info' >> /etc/csh.cshrc
fi

echo ""
echo "Checking for the Logtalk environment variable definitions..."
echo ""

if grep "LOGTALKHOME=/opt/local/share/logtalk" /etc/zprofile >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for zsh!"
else
	/bin/cp -fp /etc/zprofile /etc/zprofile.lgtsaved
	echo '' >> /etc/zprofile
	echo '# Logtalk environment variables default values' >> /etc/zprofile
	echo 'LOGTALKHOME=/opt/local/share/logtalk' >> /etc/zprofile
	echo 'LOGTALKUSER=$HOME/logtalk' >> /etc/zprofile
	echo 'export LOGTALKHOME LOGTALKUSER' >> /etc/zprofile
fi

if grep "LOGTALKHOME=/opt/local/share/logtalk" /etc/profile >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for bash!"
else
	/bin/cp -fp /etc/profile /etc/profile.lgtsaved
	echo '' >> /etc/profile
	echo '# Logtalk environment variables default values' >> /etc/profile
	echo 'LOGTALKHOME=/opt/local/share/logtalk' >> /etc/profile
	echo 'LOGTALKUSER=$HOME/logtalk' >> /etc/profile
	echo 'export LOGTALKHOME LOGTALKUSER' >> /etc/profile
fi

if grep "setenv LOGTALKHOME /opt/local/share/logtalk" /etc/csh.cshrc >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for csh!"
else
	/bin/cp -fp /etc/csh.cshrc /etc/csh.cshrc.lgtsaved
	echo '' >> /etc/csh.cshrc
	echo '# Logtalk environment variables default values' >> /etc/csh.cshrc
	echo "setenv LOGTALKHOME /opt/local/share/logtalk" >> /etc/csh.cshrc
	echo 'setenv LOGTALKUSER $HOME/logtalk' >> /etc/csh.cshrc
fi

echo ""
echo "Creating a link to the Logtalk installation folder in the /Applications folder..."
/bin/ln -sf /opt/local/share/logtalk /Applications/Logtalk
/usr/bin/open /Applications/Logtalk/
/usr/bin/open /Applications/Logtalk/scripts/macos/command_files
/usr/bin/open /Applications/Logtalk/README.md

echo ""
echo "You have successfully installed Logtalk!"
echo ""
